import ika

def GetButtons():
    """
        Returns buttons filtered by position and pressed.
    """
    return [button for button in buttons if buttons[button].Position() > 0.5], [button for button in buttons if buttons[button].Pressed()]

deadzone = 0.5
controls = dict()
key_names = ['BACKSPACE', 'TAB', 'CLEAR', 'RETURN', 'PAUSE', 'ESCAPE',
                'SPACE', 'EXCLAIM', 'QUOTEDBL', 'HASH', 'DOLLAR', 'AMPERSAND',
                'QUOTE', 'LEFTPAREN', 'RIGHTPAREN', 'ASTERISK', 'PLUS',
                'COMMA', 'MINUS', 'PERIOD', 'SLASH', '0', '1', '2', '3', '4',
                '5', '6', '7', '8', '9', 'COLON', 'SEMICOLON', 'LESS',
                'EQUALS', 'GREATER', 'QUESTION', 'AT', 'LEFTBRACKET',
                'BACKSLASH', 'RIGHTBRACKET', 'CARET', 'UNDERSCORE',
                'BACKQUOTE', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                'W', 'X', 'Y', 'Z', 'DELETE', 'KP0', 'KP1', 'KP2', 'KP3',
                'KP4', 'KP5', 'KP6', 'KP7', 'KP8', 'KP9', 'KP_PERIOD',
                'KP_DIVIDE', 'KP_MULTIPLY', 'KP_MINUS', 'KP_PLUS', 'KP_ENTER',
                'KP_EQUALS', 'UP', 'DOWN', 'RIGHT', 'LEFT', 'INSERT', 'HOME',
                'END', 'PAGEUP', 'PAGEDOWN', 'F1', 'F2', 'F3', 'F4', 'F5',
                'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14',
                'F15', 'NUMLOCK', 'CAPSLOCK', 'SCROLLOCK', 'RSHIFT', 'LSHIFT',
                'RCTRL', 'LCTRL', 'RALT', 'LALT', 'RMETA', 'LMETA', 'LSUPER',
                'RSUPER', 'MODE']

#Load up keyboard controls.
for key in key_names:
    controls["key:" + key] = ika.Input.keyboard[key]

# joystick:
for joyIndex, joy in enumerate(ika.Input.joysticks):
    # joystick axes:
    for axisIndex, axis in enumerate(joy.axes):
        controls['joy:%i:axis:%i+' % (joyIndex, axisIndex)] = axis
    for axisIndex, axis in enumerate(joy.reverseaxes):
        controls['joy:%i:axis:%i-' % (joyIndex, axisIndex)] = axis

    # joystick buttons:
    for buttonIndex, button in enumerate(joy.buttons):
        controls['joy:%i:buttons:%i' % (joyIndex, buttonIndex)] = button


class Button(object):
    def __init__(self, *keys):
        self.pressed = False
        self.keys = []
        self.key_names = []
        self.time_pressed = 0
        for key in keys:
            self.Add(key)

    def Add(self, key):
        if key in controls:
            if controls[key] not in self.keys:
                self.keys.append(controls[key])
                key_name = key.split(":")[-1]
                self.key_names.append(key_name)
                #print key_name

    def Position(self):
        values = []
        for k in self.keys:
            values.append(k.Position())
        return max(values)

    def Pressed(self):
        if not self.pressed:
            if self.Position() >= deadzone:
                self.time_pressed = ika.GetTime()
                self.pressed = True
                return True
        elif self.Position() < deadzone:
            self.pressed = False
        return False

def GetButtonName(name):
    if name in buttons:
        return name.capitalize() + " (" + buttons[name].key_names[0] + ")"
    return ""

# Controls used by the game engine!
buttons = {
    "run": Button("key:Z", "joy:0:buttons:3"),
    "attack": Button("key:X", "joy:0:buttons:2"),
    "quickmenu": Button("key:C", "joy:0:buttons:1"),
    "up": Button("key:UP", "joy:0:axis:1-"),
    "down": Button("key:DOWN", "joy:0:axis:1+"),
    "left": Button("key:LEFT", "joy:0:axis:0-"),
    "right": Button("key:RIGHT", "joy:0:axis:0+"),
    "slow" : Button("key:LSHIFT"),
    "ffwd" : Button("key:BACKQUOTE")
}
